package com.linchuan.concurrency;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import static com.linchuan.concurrency.ThreadPerTaskWebServer.handleRequest;

public class TaskExecutionWebServer {
  private static final int NTHREADS = 100;
  private static final Executor exec = Executors.newFixedThreadPool(NTHREADS);

  public static void main(String[] args) throws IOException {
    ServerSocket socket = new ServerSocket(8081);
    while (true) {
      final Socket connection = socket.accept();
      Runnable task =
          new Runnable() {
            @Override
            public void run() {
              handleRequest(connection);
            }
          };
      exec.execute(task);
    }
  }
}
